<template>
  <div class="app-container">
    <el-row type="flex" align="center" :gutter="20">
      <el-form class="filter-block" label-position="left" :inline="true" label-width="auto">
        <el-form-item label="关键字:">
          <el-input
            placeholder="场所名称 | 场所地址"
            v-model="filterData.keyword"
            maxlength="20"
            size="small"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item label="场所分类:">
          <el-cascader
            v-loading="placeTypeLoading" v-model="filterData.typeId"
            clearable
            placeholder="全部分类"
            :options="placeTypeOption"
            size="small"
            :props="{value:'id',label:'typeName',children:'sonList'}"
            filterable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="扫码时间:">
          <el-date-picker
            v-model="filterData.time"
            value-format="yyyy-MM-dd HH:mm:ss"
            size="small"
            clearable
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            align="left"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" @click="refreshTable">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-download" type="primary" @click="exportRecord">导出记录</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-table v-loading="tableConf.loading" :stripe="true" :border="true" :data="tableData">
        <el-table-column align="center" width="50" prop="id" label="ID" />
        <el-table-column align="center" prop="placeName" label="场所名称" />
        <el-table-column align="center" prop="placeAddress" label="场所地址" />
        <el-table-column align="center" prop="timeText" label="扫码时间" />
      </el-table>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-pagination
        :page-sizes="tableConf.sizes"
        :page-size="tableConf.size"
        :total="tableConf.total"
        :current-page="tableConf.page"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>
  </div>
</template>

<script>
import {getPlaceByCusList,exportDataApi} from '@/api/wxUser'
import { getPlaceTypeApi } from '@/api/placeCode'
import { createFile } from '@/utils/utils'
export default {
  name:'scanRecord',
  data() {
    return {
      tableData: [],
      tableConf: {
        page: 1,
        sizes: [10, 20, 50, 100],
        total: 0,
        size: 10,
        loading: false
      },
      filterData:{
        keyword: '',
        typeId: [],
        time:[],
      },
      placeTypeLoading: true,
      placeTypeOption: [],
    }
  },
  watch: {

  },
  mounted() {
    this.createParams(this);
    this.getTableList()
    this.getPlaceType()
  },
  methods: {
    //导出当前条件excel
    exportRecord(){
      const typeId = this.filterData.typeId.length === 0 ? 0 : this.filterData.typeId[1]
      const options={
        id: this.id,
        keyWord: this.filterData.keyword,
        startTime: this.filterData.time[0],
        endTime: this.filterData.time[1],
        typeId
      }
      this.$confirm('确定导出当前查询条件的数据吗','提示').then(()=>{
        const loading=this.$loading({ text: '处理中，请勿关闭页面' })
        exportDataApi(options).then((res)=>{
          if(!createFile(res,'人员扫码记录')){
            this.$message.error('未知的文件格式');
          }
        }).finally(()=>{
          loading.close();
        })
      })
    },
    getPlaceType() {
      getPlaceTypeApi().then((res) => {
        this.placeTypeOption = res
      }).catch((err) => {
        this.$message.error('场所分类加载失败，请重试')
      }).finally(() => {
        this.placeTypeLoading = false
      })
    },
    // 每页显示数量change
    handleSizeChange(e) {
      this.tableConf.size = e
      this.getTableList()
    },
    // 当前页change
    handleCurrentChange(e) {
      this.tableConf.page = e
      this.getTableList()
    },
    //获取列表
    getTableList() {
      this.tableConf.loading = true
      const typeId = this.filterData.typeId.length === 0 ? 0 : this.filterData.typeId[1]
      getPlaceByCusList({ page: this.tableConf.page, limit: this.tableConf.size, id:this.id,keyWord: this.filterData.keyword,
        startTime: this.filterData.time[0],
        endTime: this.filterData.time[1],
        typeId}).then((res) => {
        this.tableData = res.list;
        this.tableConf.total = res.count;
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    refreshTable() {
      this.tableConf.page = 1
      this.getTableList()
    }
  }
}
</script>
<style lang="scss" scoped>
.filterData-block {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
